/*
Disclaimer of Warranty: THE PACKAGE IS PROVIDED
BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS'
AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT
ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW.
UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF
THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


(c) Copyright 2013 Edward Principe
All Rights Reserved

*/
#include "stdafx.h"
#include "tokmod.h"

///////////////////////////////////////////////////////////////
// Set: Adds tokens to the module
//////////////////////////////////////////////////////////////
void TokMod::Set(const TokLine& rhs)
{
	_tokens.push_back(rhs);
}

void TokMod::Set(const TokNode& rhs)
{
	_buffer->Set(rhs);
}

////////////////////////////////////////////////////////////
// Commit: Adds the buffer to the end of the module
///////////////////////////////////////////////////////////
void TokMod::Commit()
{
	_tokens.push_back(*_buffer);
	_buffer->Reset();
}

//////////////////////////////////////////////////////////
// Append: Appends tokens to the module
//////////////////////////////////////////////////////////
void TokMod::Append(const TokMod& rhs)
{
	for (list<TokLine>::const_iterator i = rhs._tokens.begin(); i != rhs._tokens.end(); i++)
		_tokens.push_back(static_cast<TokLine>(*i));
}

void TokMod::Append(const TokLine& rhs)
{
	_tokens.push_back(rhs);
}

void TokMod::Append(const TokNode& rhs)
{
	_buffer->Set(rhs);
}

///////////////////////////////////////////////////////
// Write: Writes tokens to file
///////////////////////////////////////////////////////
void TokMod::Write(fstream& out)
{
	// Count how many lines in the module
	unsigned long count = 0; // The line count

	for (list<TokLine>::iterator i = _tokens.begin(); i != _tokens.end(); i++)
		count++;

	// Write count
	if (!out.write(reinterpret_cast<char *>(&count), sizeof(count)))
		throw new CantWriteFile();
	
	// Write the module to file
	for (list<TokLine>::iterator i = _tokens.begin(); i != _tokens.end(); i++)
		i->Write(out);
}

/////////////////////////////////////////////////////////////////////////////
// Read: Reads in a module of tokens
/////////////////////////////////////////////////////////////////////////////
void TokMod::Read(fstream& in)
{
	// Locals
	unsigned long count = 0; // How many lines?
	SmtPtr<TokLine> buffer; // The input buffer

	// Erase tokens
	_tokens.erase(_tokens.begin(), _tokens.end());

	// Read count
	if (!in.read(reinterpret_cast<char *>(&count), sizeof(count)))
		throw new CantReadFile();

	for (unsigned long i = 0; i < count; i++)
	{
		buffer->Read(in);
		_tokens.push_back(*buffer);
		_buffer->Reset();
	}
}

/////////////////////////////////////////////////////////////////////////
// Dump: Dumps a module of tokens
/////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
void TokMod::Dump()
{
	for (list<TokLine>::iterator i = _tokens.begin(); i != _tokens.end(); i++)
		i->Dump();
}
#endif